<template>
  <div class="goods">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul>
        <li v-for="(item, index) in goods" class="menu-item"
            :class="{'current':index === currentIndex}"
            @click="selectMenu(index, $event)">
          <span class="desc">
            <i class="icon" v-show="item.type > 0" :class="classMap[item.type]"></i>
            <span class="text">{{item.name}}</span>
          </span>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref="foodsWrapper">
      <ul>
        <li v-for="item in goods" class="food-list food-list-hook">
          <h1>{{item.name}}</h1>
          <ul>
            <li @click="foodDetail(food, $event)" v-for="food in item.foods" class="food-item">
              <div class="icon" >
                <img width="57" height="57" :src="food.icon" >
              </div>
              <div class="content-wrapper">
                <span class="name">{{food.name}}</span>
                <span class="desc">{{food.description}}</span>
                <div class="extra">
                  <span class="num">月售{{food.sellCount}}份</span>
                  <span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="new-price">￥{{food.price}}</span>
                  <span class="old-price" v-if="food.oldPrice">￥{{food.oldPrice}}</span>
                </div>

                <!--food增减-->
                <div class="cart-control-wrapper">
                  <v-cart-control :food="food"></v-cart-control>
                </div>

              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <v-shop-cart :delivery-price="seller.deliveryPrice"
                 :min-price="seller.minPrice"
                 :select-foods="selectFoods"
                 >
                 <!--:select-foods="selectFoods">-->
    </v-shop-cart>

    <v-food-detail :food="currentFood" ref="detail"></v-food-detail>

  </div>

</template>

<script>
  import BScroll from 'better-scroll';
  import ShopCart from '../ShopCart/ShopCart';
  import CartControl from '../CartControl/CartControl';
  import FoodDetail from '../FoodDetail/FoodDetail';

  const NO_ERR = 0;

  export default {
    components: {
      'v-shop-cart': ShopCart,
      'v-cart-control': CartControl,
      'v-food-detail': FoodDetail
    },
    props: {
      seller: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    data () {
      return {
        goods: [],
        listHeight: [],
        scrollY: 0,
        currentFood: {},
      }
    },
    computed: {
      currentIndex() {
        for (let i = 0; i < this.listHeight.length; i++) {
          let height1 = this.listHeight[i];
          let height2 = this.listHeight[i + 1];
          if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
            return i;
          }
        }
        return 0;
      },
      selectFoods() {
        let foods = [];
        this.goods.forEach(good => {
          good.foods.forEach(food => {
            if (food.count) {
              foods.push(food);
            }
          });
        });
        return foods;
      }
    },
    created() {
      fetch('/api/goods').then((res) => {
        return res.json()
      }).then(resp => {
        if (resp.errno === NO_ERR) {
          this.goods = resp.data;
          this.$nextTick(() => {
            this._initScroll();
            this._calculateHeight();
          });
        }
      });

      this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
    },
    methods: {
      _initScroll() {
        this.menuScroll = new BScroll(this.$refs.menuWrapper, {
          click: true
        });
        this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
          click: true,
          probeType: 3  // 探针，可以探测滚动的位置
        });

        this.foodsScroll.on('scroll', (pos) => {
          this.scrollY = Math.abs(Math.round(pos.y));
        });
      },
      _calculateHeight() {
        let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook');
        let height = 0;
        this.listHeight.push(height);

        for (let i = 0; i < foodList.length; i++) {
          let item = foodList[i];
          height += item.clientHeight;
          this.listHeight.push(height);
        }
      },
      selectMenu(index, event) {
        if (!event._constructed) {
          return;
        }
        let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook');
        let el = foodList[index];
        this.foodsScroll.scrollToElement(el, 300);
      },
      foodDetail(food, event) {
        if (!event._constructed) {
          return;
        }
        this.currentFood = food;
        // 调用子组件FoodDetail中的方法
        this.$refs.detail.showDetail();
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
  @import "../../common/css/minxin";
  @import "../../common/css/icon";

  .goods {
    position: absolute;
    display: flex;
    width: 100%;
    left: 0;
    top: 174px;
    bottom: 46px;
    overflow: hidden;
    .menu-wrapper {
      flex: 0 0 80px;
      width: 80px;
      background: #f3f5f7;
      .menu-item {
        display: table; /*for 垂直居中*/
        padding: 0 12px;
        width: 56px;
        height: 54px;
        line-height: 14px;
        .desc {
          display: table-cell;
          width: 56px;
          vertical-align: middle;
          .border-1px(rgba(7, 17, 27, .1));
          font-size: 0;
          .icon {
            display: inline-block;
            width: 12px;
            height: 12px;
            margin-right: 2px;
            vertical-align: top;
            background-size: 12px 12px;
            background-repeat: no-repeat;
            &.decrease {
              .bg-image("./img/decrease_3");
            }
            &.discount {
              .bg-image("./img/discount_3");
            }
            &.guarantee {
              .bg-image("./img/guarantee_3");
            }
            &.invoice {
              .bg-image("./img/invoice_3");
            }
            &.special {
              .bg-image("./img/special_3");
            }
          }
          .text {
            line-height: 14px;
            font-size: 12px;
          }
        }
      }
      .current {
        position: relative;
        margin-top: -1px;
        z-index: 10;
        font-weight: 700;
        background: #fff;
        .desc {
          .border-none();
        }

      }
    }
    .foods-wrapper {
      flex: 1;
      background: #fff;
      .food-list {
        h1 {
          padding-left: 14px;
          height: 26px;
          line-height: 26px;
          border-left: 2px solid #d9dde1;
          font-size: 12px;
          color: rgb(147, 153, 159);
          background: #f3f5f7;
        }
        .food-item {
          display: flex;
          margin: 18px;
          padding-bottom: 18px;
          .border-1px(rgba(7, 17, 27, .1));
          &:last-child {
            .border-none();
          }
          .icon {
            flex: 0 0 57px;
            margin-right: 10px;
          }
          .content-wrapper {
            flex: 1;
            padding-top: 2px;
            font-size: 0;
            .name {
              display: block;
              margin-bottom: 8px;
              font-size: 14px;
              color: rgb(7, 17, 27);
              line-height: 14px;
              height: 14px;
            }
            .desc {
              display: block;
              margin-bottom: 8px;
              font-size: 10px;
              color: rgb(147, 153, 159);
              line-height: 12px;
            }
            .extra {
              font-size: 0;
              color: rgb(147, 153, 159);
              line-height: 10px;
              span {
                font-size: 10px;
              }
              .num {
                margin-right: 12px;
              }
            }
            .price {
              line-height: 24px;
              .new-price {
                margin-right: 8px;
                font-size: 14px;
                font-weight: 700;
                color: rgb(240, 20, 20);
              }
              .old-price {
                text-decoration: line-through;
                font-size: 10px;
                font-weight: normal;
                color: rgb(147, 153, 159);
              }
            }
            .cart-control-wrapper {
              position: absolute;
              right: 0;
              bottom: 12px;
            }
          }
        }
      }
    }


  }
</style>
